package com.atung.parking.mapstruct;

import com.atung.parking.entity.BusApprovalRecord;
import com.atung.parking.entity.BusParkingIot;
import com.atung.parking.entity.BusParkingIotPackage;
import com.atung.parking.entity.BusSuperUser;
import com.atung.parking.enums.ApplicableScopeType;
import com.atung.parking.enums.PackageSourceType;
import com.atung.parking.enums.ParkingPackageState;
import com.atung.parking.enums.ParkingPackageType;
import com.atung.parking.export.ParkingIotPackageExportVO;
import com.atung.parking.mapper.BusParkingIotMapper;
import com.atung.parking.mapper.BusSuperUserMapper;
import com.atung.parking.response.page.ApprovalRecordResponse;
import com.atung.parking.response.page.ParkingIotPackageResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author Administrator
 */

@Component
public class ParkingIotPackageConverterDecorator implements ParkingIotPackageConverter {

    @Resource
    private BusSuperUserMapper busSuperUserMapper;
    @Resource
    private BusParkingIotMapper busParkingIotMapper;

    @Override
    public ParkingIotPackageResponse toResponse(BusParkingIotPackage busParkingIotPackage) {
        if (busParkingIotPackage == null) {
            return null;
        }

        ParkingIotPackageResponse parkingIotPackageResponse = new ParkingIotPackageResponse();

        parkingIotPackageResponse.setId(busParkingIotPackage.getId());
        parkingIotPackageResponse.setPackageType(ParkingPackageType.getDescriptionByName(busParkingIotPackage.getPackageType()));
        parkingIotPackageResponse.setValidPeriodStart(busParkingIotPackage.getValidPeriodStart());
        parkingIotPackageResponse.setValidPeriodEnd(busParkingIotPackage.getValidPeriodEnd());
        parkingIotPackageResponse.setApplicableScope(ApplicableScopeType.getDescriptionByName(busParkingIotPackage.getApplicableScope()));
        parkingIotPackageResponse.setPackagePrice(busParkingIotPackage.getPackagePrice());
        parkingIotPackageResponse.setOriginalPrice(busParkingIotPackage.getOriginalPrice());
        parkingIotPackageResponse.setStock(busParkingIotPackage.getStock());
        parkingIotPackageResponse.setState(ParkingPackageState.getDescriptionByCode(busParkingIotPackage.getState()));
        parkingIotPackageResponse.setDataSource(PackageSourceType.getDescriptionByName(busParkingIotPackage.getDataSource()));
        if (StringUtils.hasText(busParkingIotPackage.getCreatorTime())) {
            try {
                long timeInMillis = Long.parseLong(busParkingIotPackage.getCreatorTime());
                Date date = new Date(timeInMillis);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = sdf.format(date);
                parkingIotPackageResponse.setCreatorTime(format);
            } catch (NumberFormatException e) {
                // 如果timestamp不是有效的长整型数字字符串，返回null或抛出异常
                parkingIotPackageResponse.setCreatorTime("时间格式化异常");
            }
        }
        //查询停车场
        BusParkingIot busParkingIot = busParkingIotMapper.selectById(busParkingIotPackage.getParkingIotId());
        if (Objects.nonNull(busParkingIot)) {
            parkingIotPackageResponse.setParkingIotName(busParkingIot.getName());
        }
        //查询客户
        BusSuperUser busSuperUser = busSuperUserMapper.selectById(busParkingIotPackage.getSuperUserId());
        if (Objects.nonNull(busSuperUser)) {
            parkingIotPackageResponse.setSuperUserName(busSuperUser.getName());
        }
        return parkingIotPackageResponse;
    }

    @Override
    public ParkingIotPackageExportVO toExportVo(BusParkingIotPackage busParkingIotPackage) {
        if (busParkingIotPackage == null) {
            return null;
        }
        ParkingIotPackageExportVO parkingIotPackageExportVO = new ParkingIotPackageExportVO();
        parkingIotPackageExportVO.setId(busParkingIotPackage.getId());
        parkingIotPackageExportVO.setPackageType(ParkingPackageType.getDescriptionByName(busParkingIotPackage.getPackageType()));
        parkingIotPackageExportVO.setValidPeriodStart(busParkingIotPackage.getValidPeriodStart());
        parkingIotPackageExportVO.setValidPeriodEnd(busParkingIotPackage.getValidPeriodEnd());
        parkingIotPackageExportVO.setApplicableScope(ApplicableScopeType.getDescriptionByName(busParkingIotPackage.getApplicableScope()));
        parkingIotPackageExportVO.setPackagePrice(busParkingIotPackage.getPackagePrice());
        parkingIotPackageExportVO.setOriginalPrice(busParkingIotPackage.getOriginalPrice());
        parkingIotPackageExportVO.setStock(busParkingIotPackage.getStock());
        parkingIotPackageExportVO.setState(ParkingPackageState.getDescriptionByCode(busParkingIotPackage.getState()));
        parkingIotPackageExportVO.setDataSource(PackageSourceType.getDescriptionByName(busParkingIotPackage.getDataSource()));
        if (StringUtils.hasText(busParkingIotPackage.getCreatorTime())) {
            try {
                long timeInMillis = Long.parseLong(busParkingIotPackage.getCreatorTime());
                Date date = new Date(timeInMillis);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = sdf.format(date);
                parkingIotPackageExportVO.setCreatorTime(format);
            } catch (NumberFormatException e) {
                // 如果timestamp不是有效的长整型数字字符串，返回null或抛出异常
                parkingIotPackageExportVO.setCreatorTime("时间格式化异常");
            }
        }
        //查询停车场
        BusParkingIot busParkingIot = busParkingIotMapper.selectById(busParkingIotPackage.getParkingIotId());
        if (Objects.nonNull(busParkingIot)) {
            parkingIotPackageExportVO.setParkingIotName(busParkingIot.getName());
        }
        //查询客户
        BusSuperUser busSuperUser = busSuperUserMapper.selectById(busParkingIotPackage.getSuperUserId());
        if (Objects.nonNull(busSuperUser)) {
            parkingIotPackageExportVO.setSuperUserName(busSuperUser.getName());
        }
        return parkingIotPackageExportVO;

    }

    @Override
    public ApprovalRecordResponse toApprovalResponse(BusApprovalRecord busApprovalRecord) {

        if (busApprovalRecord == null) {
            return null;
        }

        ApprovalRecordResponse approvalRecordResponse = new ApprovalRecordResponse();

        approvalRecordResponse.setId(busApprovalRecord.getId());
        approvalRecordResponse.setApprovalType(busApprovalRecord.getApprovalType());
        approvalRecordResponse.setCreatorName(busApprovalRecord.getCreatorName());

        if (StringUtils.hasText(busApprovalRecord.getCreatorTime())) {
            try {
                long timeInMillis = Long.parseLong(busApprovalRecord.getCreatorTime());
                Date date = new Date(timeInMillis);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = sdf.format(date);
                approvalRecordResponse.setCreatorTime(format);
            } catch (NumberFormatException e) {
                // 如果timestamp不是有效的长整型数字字符串，返回null或抛出异常
                return null;
            }
        }
        return approvalRecordResponse;
    }
}
